import { parseTime } from '@/utils/common';

/**
 * 运维工作台二级页面折线图默认样式
 */
export function getLineStyle() {
	return {
		// 无数据时统一样式
		noDataOption: {
			title: {
				text: '暂无数据',
				x: 'center',
				y: 'center',
				textStyle: {
					color: '#ffffff70',
				},
			},
			// 坐标系
			grid: {
				x: 60,
				x2: 20,
				y: 10,
				y2: 20,
			},
			xAxis: {
				axisLine: {
					show: true,
					lineStyle: {
						color: '#ffffff',
					},
				},
			},
			yAxis: {
				axisLine: {
					show: true,
					lineStyle: {
						color: '#ffffff',
					},
				},
				nameTextStyle: {
					padding: [10, 10, 10, 30],
				},
			},
		},
		// X轴统一样式
		xAxisStyle: {
			type: 'category',
			// X坐标轴样式
			axisLine: {
				lineStyle: {
					color: '#ffffff',
				},
			},
			// X轴刻度样式
			axisTick: {
				show: false,
			},
		},
		// Y轴统一样式
		yAxisStyle: {
			type: 'value',
			splitNumber: 5,
			axisLabel: {
				margin: 6,
			},
			// Y坐标轴样式
			axisLine: {
				show: true,
				lineStyle: {
					color: '#ffffff',
				},
			},
			// 分隔线样式
			splitLine: {
				show: false,
				lineStyle: {
					opacity: 0.2,
				},
			},
			// 最小间隔(保证整数)
			minInterval: 1,
		},
	};
}

/**
 * 运维工作台折线图区块颜色
 * */
export function getAreaStyle(color: string, opacityMaxNum: number | undefined = undefined, opacityMinNum: number | undefined = undefined) {
	const opacityMax = opacityMaxNum || 0.4;
	const opacityMin = opacityMinNum || 0;
	return {
		// 填充区域
		areaStyle: {
			color: {
				type: 'linear',
				x: 0,
				x2: 0,
				y: 0,
				y2: 1,
				colorStops: [
					{
						offset: 0,
						color: 'rgba(' + color.substring(color.indexOf('(') + 1, color.indexOf(')')) + ',' + opacityMax + ')',
					},
					{
						offset: 1,
						color: 'rgba(' + color.substring(color.indexOf('(') + 1, color.indexOf(')')) + ',' + opacityMin + ')',
					},
				],
			},
		},
	};
}

/**
 * 自适应echarts图表
 * */
export function chartResize(chartsObject: anyObj) {
	Object.keys(chartsObject).forEach((key) => {
		chartsObject[key]?.resize();
	});
}

/**
 * 省地理位置数据
 * */
export function getGeo(place: string) {
	let geoCoordMap = {};
	switch (place) {
		case 'jiangxi':
			geoCoordMap = {
				南昌市: [115.892151, 28.676493],
				景德镇市: [117.214664, 29.29256],
				萍乡市: [113.852186, 27.622946],
				九江市: [115.954526, 29.573672],
				新余市: [114.930835, 27.810834],
				鹰潭市: [117.033838, 28.238638],
				赣州市: [114.940278, 25.85097],
				吉安市: [114.986373, 27.111699],
				宜春市: [114.882917, 28.464105],
				抚州市: [116.358351, 27.48385],
				上饶市: [117.971185, 28.44442],
			};
			break;
		case 'guangdong':
			geoCoordMap = {
				广州市: [113.507649675, 23.3200491021],
				东莞市: [113.863433991, 22.9950238154],
				中山市: [113.422060021, 22.5451775145],
				云浮市: [111.750945959, 22.9379756855],
				佛山市: [113.034025635, 23.0350948405],
				惠州市: [114.41065808, 23.0535398524],
				揭阳市: [116.079500855, 23.3479994669],
				梅州市: [116.126403098, 24.304570606],
				汕头市: [116.588650288, 23.2839084533],
				汕尾市: [115.572924289, 22.9787305002],
				江门市: [112.678125341, 22.2751167835],
				河源市: [114.913721476, 23.9572508505],
				深圳市: [114.025973657, 22.5960535462],
				清远市: [113.040773349, 23.9984685504],
				湛江市: [110.165067263, 21.2574631038],
				潮州市: [116.830075991, 23.8618116765],
				珠海市: [113.282447026, 22.1669146461],
				肇庆市: [112.37965337, 23.5786632829],
				茂名市: [110.931245331, 21.9682257188],
				阳江市: [111.777009756, 22.0515173045],
				韶关市: [113.594461107, 24.8029603119],
			};
			break;
		case 'sichuan':
			geoCoordMap = {
				成都市: [104.065735, 30.659462],
				自贡市: [104.773447, 29.352765],
				攀枝花市: [101.716007, 26.580446],
				泸州市: [105.443348, 28.889138],
				德阳市: [104.398651, 31.127991],
				绵阳市: [104.741722, 31.46402],
				广元市: [105.829757, 32.433668],
				遂宁市: [105.571331, 30.513311],
				内江市: [105.066138, 29.58708],
				乐山市: [103.761263, 29.582024],
				南充市: [106.082974, 30.795281],
				眉山市: [103.831788, 30.048318],
				宜宾市: [104.630825, 28.760189],
				广安市: [106.633369, 30.456398],
				达州市: [107.502262, 31.209484],
				雅安市: [103.001033, 29.987722],
				巴中市: [106.753669, 31.858809],
				资阳市: [104.641917, 30.122211],
				阿坝藏族羌族自治州: [102.221374, 31.899792],
				甘孜藏族自治州: [101.963815, 30.050663],
				凉山彝族自治州: [102.258746, 27.886762],
			};
			break;
		case 'shandong':
			geoCoordMap = {
				济南市: [117.121225, 36.66466],
				菏泽市: [115.480656, 35.23375],
				济宁市: [117.129063, 36.194968],
				德州市: [116.39, 37.45],
				聊城市: [115.97, 36.45],
				泰安市: [117.13, 36.18],
				临沂市: [118.35, 35.05],
				淄博市: [118.05, 36.78],
				枣庄市: [117.57, 34.86],
				滨州市: [118.03, 37.36],
				潍坊市: [119.1, 36.62],
				东营市: [118.49, 37.46],
				青岛市: [120.3, 36.62],
				烟台市: [120.9, 37.32],
				威海市: [122.1, 37.2],
				日照市: [119.1, 35.62],
				莱芜市: [117.7, 36.28],
			};
			break;
		case 'xinjiang':
			geoCoordMap = {
				巴音郭楞蒙古: [88.1653, 39.6002],
				和田: [81.167, 36.9855],
				哈密: [93.7793, 42.9236],
				阿克苏: [82.9797, 41.0229],
				阿勒泰: [88.2971, 47.0929],
				喀什: [77.168, 37.8534],
				塔城: [86.6272, 45.8514],
				昌吉: [89.6814, 44.4507],
				克孜勒苏柯尔克孜: [74.6301, 39.5233],
				吐鲁番: [89.6375, 42.4127],
				伊犁哈萨克: [82.5513, 43.5498],
				博尔塔拉蒙古: [81.8481, 44.6979],
				乌鲁木齐: [87.9236, 43.5883],
				克拉玛依: [85.2869, 45.5054],
				阿拉尔: [81.2769, 40.6549],
				图木舒克: [79.1345, 39.8749],
				五家渠: [87.5391, 44.3024],
				石河子: [86.0229, 44.2914],
			};
			break;
		case 'hubei':
			geoCoordMap = {
				十堰市: [110.505076, 32.3907915],
				荆州市: [112.661799, 30.0451365],
				荆门市: [112.6694995, 31.0027585],
				襄阳市: [111.9355865, 31.9309895],
				鄂州市: [114.796437, 30.3183775],
				随州市: [113.42538, 31.8812465],
				黄冈市: [115.26422, 30.664937],
				黄石市: [115.015914, 29.9212475],
				咸宁市: [114.250381, 29.6665605],
				天门市: [113.0242125, 30.643268],
				孝感市: [113.9487345, 31.119153],
				宜昌市: [111.1645635, 30.7619465],
				恩施土家族苗族自治州: [109.500639, 30.2582235],
				武汉市: [114.3859335, 30.6665495],
				潜江市: [112.762119, 30.3637185],
				神农架林区: [110.456263, 31.5808695],
			};
			break;
		case 'anhui':
			geoCoordMap = {
				合肥市: [117.37, 31.386],
				六安市: [116.27, 31.786],
				滁州市: [118.07, 32.486],
				蚌埠市: [117.27, 33.086],
				淮南市: [116.67, 32.786],
				宿州市: [117.77, 33.486],
				淮北市: [116.67, 33.686],
				亳州市: [116.27, 33.386],
				阜阳市: [115.57, 32.986],
				安庆市: [116.47, 30.486],
				池州市: [117.47, 30.386],
				黄山市: [118.17, 29.886],
				宣城市: [119.27, 30.916],
				芜湖市: [118.17, 31.186],
				马鞍山市: [118.47, 31.616],
				铜陵市: [117.87, 30.956],
			};
			break;
		case 'jiangsu':
			geoCoordMap = {
				南京市: [118.767413, 32.041544],
				无锡市: [120.301663, 31.574729],
				徐州市: [117.184811, 34.261792],
				常州市: [119.946973, 31.772752],
				苏州市: [120.619585, 31.299379],
				南通市: [120.864608, 32.016212],
				连云港市: [119.178821, 34.600018],
				淮安市: [119.021265, 33.597506],
				盐城市: [120.139998, 33.377631],
				扬州市: [119.421003, 32.393159],
				镇江市: [119.452753, 32.204402],
				泰州市: [119.915176, 32.484882],
				宿迁市: [118.275162, 33.963008],
			};
			break;
		case 'fujian':
			geoCoordMap = {
				福州市: [119.306239, 26.075302],
				厦门市: [118.11022, 24.490474],
				泉州市: [118.589421, 24.908853],
				漳州市: [117.661801, 24.510897],
				宁德市: [119.527082, 26.65924],
				莆田市: [119.007558, 25.431011],
				南平市: [118.178459, 26.635627],
				三明市: [117.635001, 26.265444],
				龙岩市: [117.02978, 25.091603],
			};
			break;
	}
	return geoCoordMap;
}

// 城市数据缺时补全数据
export function fillCity(res: anyObj, geoCoordMap: anyObj) {
	const cityArr = [],
		resCityArr: string[] = [];
	res.forEach((r: anyObj) => {
		resCityArr.push(r.areaName);
	});
	for (const city in geoCoordMap) {
		cityArr.push(city);
	}
	cityArr.forEach((city) => {
		if (resCityArr.indexOf(city) < 0) {
			res.push({
				areaName: city,
			});
		}
	});
	res.forEach((r: anyObj) => {
		if (!r.areaName) {
			r.areaName = '未知';
		}
	});
}

// 获取趋势图相应的请求开始和结束时间
export function getTrendTimeRange(trendType: number, dataObj: anyObj) {
	const currentDate = new Date();
	dataObj.endTime = parseTime(currentDate.getTime(), '{y}-{m}-{d} {h}:{i}:00');
	switch (trendType) {
		case 1:
			dataObj.startTime = parseTime(currentDate.getTime() - 1000 * 3600, '{y}-{m}-{d} {h}:{i}:00');
			break;
		case 4:
			dataObj.startTime = parseTime(currentDate.getTime() - 1000 * 3600 * 4, '{y}-{m}-{d} {h}:{i}:00');
			break;
		case 8:
			dataObj.startTime = parseTime(currentDate.getTime() - 1000 * 3600 * 8, '{y}-{m}-{d} {h}:{i}:00');
			break;
		case 12:
			dataObj.startTime = parseTime(currentDate.getTime() - 1000 * 3600 * 12, '{y}-{m}-{d} {h}:{i}:00');
			break;
		case 2:
			dataObj.startTime = parseTime(currentDate.getTime(), '{y}-{m}-{d}') + ' 00:00:00';
			break;
		default:
			dataObj.startTime = parseTime(currentDate.getTime() - 1000 * 3600, '{y}-{m}-{d} {h}:{i}:00');
	}
}
